package com.yunshipei.ui.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.yunshipei.ui.activity.AlarmSetActivity;
import com.yunshipei.enterplorer.R;
import com.yunshipei.model.NotifiContextModel;
import com.yunshipei.model.NotifyInfo;

//import de.greenrobot.event.EventBus;
import io.rong.eventbus.EventBus;
/**
 * Created by cike on 15/12/10.
 */
public class NotifiContextEditFragment extends Fragment implements View.OnClickListener {
    private View view;
    private EditText editContext;
    private Button affirm;
    private NotifyInfo notifiInfo;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        AlarmSetActivity alarmSetActivity = (AlarmSetActivity) getActivity();
        notifiInfo = alarmSetActivity.getNotifiInfo();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        view = inflater.inflate(R.layout.notifi_context_edit_layout, null);
        initView();
        return view;
    }

    public void initView() {
        if (notifiInfo != null) {
            editContext.setText(notifiInfo.getSpeechRecognition().getContent());
        }
        editContext = (EditText) view.findViewById(R.id.edit_notifi_context);
        affirm = (Button) view.findViewById(R.id.affirm);
        affirm.setOnClickListener(this);
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.affirm:
                String contextStr = editContext.getText().toString();
                if (!TextUtils.isEmpty(contextStr)) {
                    NotifiContextModel ncm = new NotifiContextModel();
                    ncm.setContext(contextStr);
                    EventBus.getDefault().post(ncm);
                    getActivity().finish();
                } else {
                    Toast.makeText(getActivity(), "提醒内容不能为空", Toast.LENGTH_LONG).show();
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

}
